/*
 * Copyright (c) 2019. Mikes. All rights reserved.
 *
 * You can contact me with sending email to bWlrZXNfMjAxMkAxNjMuY29t for more information!
 */

package com.climber.android.commonsdk.util;
import android.app.FragmentManager;
import android.content.Context;
import android.widget.TextView;

import java.util.Calendar;

import mirko.android.datetimepicker.date.DatePickerDialog;
import mirko.android.datetimepicker.time.TimePickerDialog;

/**
 * Created by Mikes on 2016-8-23.
 */
public class DateTimeUtils {

    private static final Calendar mCalendar = Calendar.getInstance();
    public static int hourOfDay = mCalendar.get(Calendar.HOUR_OF_DAY);
    public static int minute = mCalendar.get(Calendar.MINUTE);
    public static int day = mCalendar.get(Calendar.DAY_OF_MONTH);
    public static int month = mCalendar.get(Calendar.MONTH);
    public static int year = mCalendar.get(Calendar.YEAR);

    public static void resetDate(Context context, TextView tv) {
        tv.setText(new StringBuilder().append(wrapper(day))
                .append(" ").append(wrapper(month + 1)).append(" ").append(wrapper(year)));
        tv.setTextColor(context.getResources().getColor(android.R.color.darker_gray));
    }

    public static String wrapper(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    public static String wrapper24(int c) {
        if (c == 12)
            return String.valueOf(c);
        if (c == 00)
            return String.valueOf(c + 12);
        if (c > 12)
            return String.valueOf(c - 12);
        else
            return String.valueOf(c);
    }

    public static String wrapper12(int c) {
        if (c == 12)
            return " PM";
        if (c == 00)
            return " AM";
        if (c > 12)
            return " PM";
        else
            return " AM";
    }

    public static void showDatePickerDialog(final Context context, FragmentManager fragmentManager, String tag, DatePickerDialog.OnDateSetListener onDateSetListener) {
        DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(onDateSetListener, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show(fragmentManager, tag);
    }

    public static void showTimePickerDialog24h(final Context context, FragmentManager fragmentManager, String tag, TimePickerDialog.OnTimeSetListener onTimeSetListener) {
        TimePickerDialog timePickerDialog24h = TimePickerDialog.newInstance(onTimeSetListener, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), true);
        timePickerDialog24h.setCancelable(false);
        timePickerDialog24h.show(fragmentManager, tag);
    }

    public static void showTimePickerDialog12h(final Context context, FragmentManager fragmentManager, String tag, TimePickerDialog.OnTimeSetListener onTimeSetListener) {
        TimePickerDialog timePickerDialog12h = TimePickerDialog.newInstance(onTimeSetListener, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), false);
        timePickerDialog12h.setCancelable(false);
        timePickerDialog12h.show(fragmentManager, tag);
    }

    /**
     * 与当前时间比较
     *
     * @param inputYear  年
     * @param inputMonth 月
     * @param inputDay   日
     * @return 1 --大于当前日期,  -1 --小于当前日期, 0 ==等于当前时间
     */
    public static int compairDate(int inputYear, int inputMonth, int inputDay) {
        String inputDate = "" + inputYear + inputMonth + inputDay;
        String currentDate = "" + year + month + day;
        if (Integer.parseInt(inputDate) < Integer.parseInt(currentDate)) {
            return -1;
        } else if (Integer.parseInt(inputDate) == Integer.parseInt(currentDate)) {
            return 0;
        } else {
            return 1;
        }
    }

    public static int compairTime(int inputHour, int inputMinute) {
        String inputTime = "" + inputHour + inputMinute;
        String currentTime = "" + hourOfDay + minute;
        if (Integer.parseInt(inputTime) < Integer.parseInt(currentTime)) {
            return -1;
        } else if (Integer.parseInt(inputTime) == Integer.parseInt(currentTime)) {
            return 0;
        } else {
            return 1;
        }
    }

}

